{% extends 'common/base.html' %}
{% block css %}
    <style>
        #face {
            opacity: 0;
            width: 100px;
            height: 100px;
        }

        #preimg {
            width: 100px;
            position: absolute;
            top: 0px;
        }
    </style>
{% endblock %}
{% block content %}
    <section class="content-header">
        <div>
            <ol class="breadcrumb">
                <li><a href="#"><i class="fa fa-dashboard"></i> 用户管理</a></li>
                <li class="active">添加用户</li>
            </ol>
        </div>
    </section>
    <section class="content" id="showcontent">
        <div class="row">
            <div class="col-md-12">
                <div class="box box-primary">
                    <div class="box-header with-border">
                        <h3 class="box-title">添加用户</h3>
                    </div>
                    <form role="form" method="post" class="form-horizontal" enctype="multipart/form-data">
                        <div class="box-body">
                            <div class="form-group">
                                <label for="input_context" class="control-label col-xs-4">
                                    {{ form.username.label }}</label>
                                <div class="col-xs-4">
                                    {{ form.username }}
                                </div>
                                {% for err in form.username.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group">
                                <label for="input_email" class="control-label col-xs-4">
                                    {{ form.email.label }}</label>
                                <div class="col-xs-4">
                                    {{ form.email }}
                                </div>
                                {% for err in form.email.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group">
                                <label for="input_phone" class="control-label col-xs-4">
                                    {{ form.phone.label }}</label>
                                <div class="col-xs-4">
                                    {{ form.phone }}
                                </div>
                                {% for err in form.phone.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group">
                                <label for="input_info" class="control-label col-xs-4">
                                    {{ form.info.label }}</label>
                                <div class="col-xs-4">
                                    {{ form.info }}
                                </div>
                                {% for err in form.info.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group">
                                <label for="input_face" class="control-label col-xs-4">
                                    {{ form.face.label }}</label>
                                <div class="col-xs-4">
                                    <span id="imgSpan">
                                        <img src="{{ url_for('static',filename='img/add.png') }}" id="preimg"/>
                                        {{ form.face }}
                                    </span>
                                </div>
                                {% for err in form.face.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                            <div class="form-group">
                                <label for="input_role_id" class="control-label col-xs-4">
                                    {{ form.role_id.label }}</label>
                                <div class="col-xs-4">
                                    {{ form.role_id }}
                                </div>
                                {% for err in form.role_id.errors %}
                                    <div class="col-md-12">
                                        <p style="color: red">{{ err }}</p>
                                    </div>
                                {% endfor %}
                            </div>
                        </div>
                        <div class="box-footer" style="text-align: center">
                            {{ form.csrf_token }}
                            {{ form.submit }}
                            <a href="{{ url_for("main.user_list") }}" class="float-right btn">返回</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </section>
{% endblock %}
{% block js %}
    <script>
        $(function () {
            var role = {{ session['role_id'] }};
            if (role != 1) {
                $("#role_id option[value='3']").prop('selected', true);
                $("#role_id option[value='1']").prop('disabled', true);
                $("#role_id option[value='2']").prop('disabled', true);
            }

        })
        $('#face').change(function () {
            var file = this.files[0];
            var fr = new FileReader();
            fr.readAsDataURL(file);
            fr.onload = function () {
                $('#preimg').attr('src', fr.result)
            }
        })
    </script>
{% endblock %}